/**
 * @description 会员 services
 */
const _ = require('lodash')
const Member = require('../db/model/Member')

/**
 * 获取会员信息
 * @param {number} userId 用户 ID
 */
async function getMemberInfo(userId) {
  const memberInfo = await Member.findOne({
    attributes: ['id', 'lastClockDate', 'clockCount'],
    where: { userId },
    raw: true,
  })
  return memberInfo;
}

/**
 * 更新会员信息
 * @param {object} updateData 更新字段对象
 * @param {number} userId 用户 ID
 */
async function updateMemberInfo(updateData, userId) {
  const toUpdate = {}
  if (!_.isUndefined(updateData.lastClockDate)) { toUpdate.lastClockDate = updateData.lastClockDate }
  if (!_.isUndefined(updateData.clockCount)) { toUpdate.clockCount = updateData.clockCount }

  const result = await Member.update(toUpdate, {
    where: { userId }
  })
  return result[0] === 1 // 通过判断更新行数来判断操作成功与否
}

module.exports = {
  getMemberInfo,
  updateMemberInfo,
}
